<?php
/**
 * Created by PhpStorm.
 * User: mars
 * Date: 2019/1/24
 * Time: 1:05 PM
 */

namespace api\models;


use api\models\event\OrderEvent;
use common\libs\Constants;
use yii\web\BadRequestHttpException;

class Order extends \common\models\Order
{
    const EVENT_PAY_SUCCESS = "pay_success";
    const EVENT_PAY_FAIL = "pay_fail";


    public function fields()
    {
        return [
            'id',
            'order_id',
            'uid',
            'status',
            'status_map' => function () {
                return Constants::getOrderStatus();
            },
            'job'        => function ($model) {
                /** @var Order $model */
                if ($model->type == self::TYPE_MISSION_POST) {
                    return Mission::findOne($model->job_id);
                }
                if ($model->type == self::TYPE_PART_TIME_POST) {
                    return PartTimeJob::findOne($model->job_id);
                }

                return '';
            },
            'good'       => function ($model) {
                /** @var Order $model */
                return $model->good != null ? $model->good : '';
            },
            'type'       => function ($model) {
                /** @var Order $model */
                return isset($model->type) ? Constants::getOrderType($model->type) : '';
            },
            'number',
            'total_price',
            'notify',
            'created_at' => function ($model) {
                /** @var Order $model */
                return \Yii::$app->formatter->asDatetime($model->created_at);
            }
        ];
    }

    /**
     * pay_success事件的handler方法
     *
     * @param OrderEvent $event
     *
     */
    public static function paySuccess($event)
    {
        //TODO:使用事务进行优化
//        \Yii::warning($event);
        $order                 = $event->order;
        $order->status         = Order::STATUS_PAY_SUCCESS;
        $consumption           = new UserConsumption(); //触发流水
        $consumption->order_id = $order->order_id;
        $consumption->uid      = $order->uid;
        $consumption->type     = UserConsumption::TYPE_WX;
        $consumption->money    = $order->total_price;
        $consumption->save();
        $auth = \Yii::$app->authManager;
//        return var_dump($order->good);
        \Yii::$app->on(Mission::EVENT_AFTER_POST, 'afterPostHandler');
        //TODO:数组重构,使用DI重写
//        \Yii::warning($order->type);
        switch ($order->type) {
            //兼职会员
            case Order::TYPE_PART_TIME_VIP:
//                User::assignVipRight(\Yii::$app->getUser()->id, $order->good);
                VipAssignment::assignRightFromOrderToUser($order);
                break;
            //兼职发布（特殊颜色）
            case Order::TYPE_PART_TIME_POST:
//                \Yii::warning("mehtod invoke!! $order->job_id");
                PartTimeJob::postPartTime($order->job_id);
                break;
            //任务发布
            case Order::TYPE_MISSION_POST:
                Mission::postMission($order->job_id);
                break;
            //接受任务
            case Order::TYPE_TAKE_MISSION:
                Mission::takeMission($order->job_id, $order->uid);
                break;
            //兼职人数查看
            case Order::TYPE_PART_TIME_SIGN_UP_CHARGE:
                Job::signUpCharge($order->job_id, $order);
                break;

            //任务订阅
            case  Order::TYPE_MISSION_SMS:
                VipAssignment::assignRightFromOrderToUser($order);
                break;

        }
        $order->save(false);
    }


}